﻿using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.SheepFarmes
{
    public class SheepFarmsBatchDeletedHandler : IRequestHandler<SheepFarmsBatchDeletedCommand, Result>
    {
        private readonly IBaseRepository<SheepFarm> _sheepFarmRepository;
        private readonly IMapper _mapper;

        public SheepFarmsBatchDeletedHandler(IBaseRepository<SheepFarm> sheepFarmRepository, IMapper mapper)
        {
            _sheepFarmRepository = sheepFarmRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepFarmsBatchDeletedCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Deleted };
            try
            {
                var sheepFarms = await(await _sheepFarmRepository.GetAll()).Where(c => request.SheepFarmIds.Contains(c.FarmNumber)).FirstOrDefaultAsync();
                if (sheepFarms != null)
                {
                    sheepFarms.IsDeleted = true;
                    sheepFarms.DeletedOwnerId = "张三";
                    sheepFarms.DeletedTime = DateTime.Now;
                    var c = await _sheepFarmRepository.Update(sheepFarms);
                    if (c > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        result.Msg = "删除成功";
                    }
                }
                else
                {
                    result.Msg = "删除失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
